package com.smartcommunity.entity.facelib;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.util.Optional;

@Data@ApiModel(description = "人脸库人员人脸图片特征信息模型。")
public class FaceLibPersonFeature {

    @ApiModelProperty(value="ID，主键序号。支持多选查询，逗号分隔。长度<=10",name="id")
    private String id;

    @ApiModelProperty(value="人员ID。查询时支持多选，逗号分隔。关联FaceLibPerson.id,长度<=11",name="facePersonId")
    private String facePersonId;

    @ApiModelProperty(value="图片地址。新增时必填。查询只支持单选，长度<=128",name="url")
    private String url;

    @ApiModelProperty(value="人脸特征码。新增时必填。不支持该属性做查询。长度无限制",name="feature")
    private String feature;

    @ApiModelProperty(value="特征MD5(32位小写)。支持多选查询，逗号分隔，单个长度<=32",name="md5")
    private String md5;

    @ApiModelProperty(value = "创建或修改时间，后端自动填充。每次修改人员信息时，会删除原来所有后再添加新的标签关系")
    private Timestamp mrowTime;

    public FaceLibPersonFeature() {
    }

    public FaceLibPersonFeature( String facePersonId, String url) {
        this.facePersonId = facePersonId;
        this.url = url;
    }

    public void caclMd5IfHasFeature(){
        Optional.ofNullable(this.feature).filter(StringUtils::isNotBlank)
                .map(feat->DigestUtils.md5DigestAsHex(feat.getBytes(StandardCharsets.UTF_8)))
                .ifPresent(this::setMd5);
    }

    public static void main(String[] args) {
        String text = "123456", key = "";
        // MD5加密
        String encodeStr = DigestUtils.md5DigestAsHex(text.getBytes());
        System.out.println(encodeStr);
    }
}
